package cn.hsw.savings.demo.controller;


import cn.hsw.component.core.base.BaseConstant;
import cn.hsw.component.core.dto.PageDto;
import cn.hsw.component.core.dto.ResultDto;
import cn.hsw.component.core.util.ResultUtil;
import cn.hsw.component.data.page.PageUtil;
import cn.hsw.component.web.base.BaseController;
import cn.hsw.savings.demo.entity.CurdDemo;
import cn.hsw.savings.demo.qo.CurdSearchQo;
import cn.hsw.savings.demo.service.CurdDemoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李子木
 * @since 2021-11-02 15:51:00
 */
@RestController
@RequestMapping("/demo/curd-demo")
@Api(tags = "测试curddemo")
public class CurdDemoController extends BaseController {

    @Autowired
    CurdDemoService service;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    public ResultDto add(@RequestBody CurdDemo curdDemo) {
        boolean save = service.save(curdDemo);
        return ResultUtil.check(save, BaseConstant.DATA_ADD_ERROR_MSG);

    }
    @ApiOperation("删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "主键", required = true, dataType = "String"),
    })
    @PostMapping("/del")
    public ResultDto del(Long pid) {
        boolean save = service.removeById(pid);
        return ResultUtil.check(save, BaseConstant.DATA_DEL_ERROR_MSG);

    }
    @ApiOperation("修改接口")
    @PostMapping("/edit")
    public ResultDto edit(@RequestBody CurdDemo curdDemo) {

        boolean save = service.updateById(curdDemo);
        return ResultUtil.check(save, BaseConstant.DATA_UPDATE_ERROR_MSG);
    }
    @ApiOperation("列表查询")
    @PostMapping("/list")
    public ResultDto<List<CurdDemo>> list() {
        return ResultUtil.success(service.list());

    }
    @ApiOperation("分页查询")
    @PostMapping("/page")
    public ResultDto<PageDto<CurdDemo>> page(CurdSearchQo params) {

        IPage<CurdDemo> page = service.page(PageUtil.getPage(params.getPage(),params.getSize()));
        return ResultUtil.success(page.getRecords(),page.getTotal());

    }
}
